import { TAllTypes, IValue, IValueNumber, IPosition, IValueBoolean } from '../../types';
import { AFunc, IPositionValue } from '../types';
import { calculate } from '../../calculate';
import { FormulaError } from '../../formula-error';
export class Ifc extends AFunc {
  protected transformBooleanValue(val: boolean): IValueBoolean {
    return {type: 'value', dataType: 'boolean', value: val};
  }
  protected isTrue(val: IValue) {
    if(val.dataType === 'number') {
      if(val.value === 0) {
        return this.transformBooleanValue(false);
      } else {
        return this.transformBooleanValue(true);
      }
    }
    if(val.dataType === 'string') {
      if(val.value === '') {
        return this.transformBooleanValue(false);
      }
      return this.transformBooleanValue(true);
    }
    if(val.dataType === 'boolean') {
      if(!val.value) {
        return this.transformBooleanValue(false);
      } else {
        return this.transformBooleanValue(true);
      }
    }
    return this.transformBooleanValue(true);
  }
  protected transformToRangeValue(val: TAllTypes): IValue[][] {
    if(val.type === 'value') {
      return [[val]];
    }
    if(val.type === 'position') {
      let _val = this.positionVal.getValue(val);
      return [_val];
    }
    if(val.type === 'colPosition') {
      let _val: IValue[][] = [];
      let temp = this.positionVal.getValue(val);
      for(let item of temp) {
        _val.push([item]);
      }
      return _val;
    }
    if(val.type === 'rowPosition') {
      let _val: IValue[] = [];
      let temp = this.positionVal.getValue(val);
      for(let item of temp) {
        _val.push(item);
      }
      return [_val];
    }
    if(val.type === 'range') {
      return val.value.getMatrixValues() as any;
    }
    return [];
  }
  public excute(args: TAllTypes[]): TAllTypes[] {
    if(args.length !== 3) {
      throw new FormulaError('#VALUE!', {errorId: 'ff27b36b33c7'});
    }
    let isTrue = (() => {
      let vals = this.transformToRangeValue(args[0]);
      for(let val of vals) {
        for(let item of val) {
          if(!this.isTrue(item).value) {
            return false;
          }
        }
      }
      return true;
    })();
    return isTrue ? [args[1]]:[args[2]];
  }
  
}